/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package classcreator.abstractclasses;

import java.util.ArrayList;

/**
 *
 * @author Samus
 */
public class AbstractClass {

    private String nombreclase;
    private String inheritance;
    private boolean autoPK;
    private ArrayList<String> assembledClass = new ArrayList<String>();
    private ArrayList<String> assembledMethods = new ArrayList<String>();
    private ArrayList<AbstractProperty> properties = new ArrayList<AbstractProperty>();
    private ArrayList<AbstractFK> fks = new ArrayList<AbstractFK>();
    private String SQLSentenceCreate = "";
    private String SQLSentenceAdd = "";
    private String SQLSentenceDelete = "";
    private String SQLSentenceModify = "";
    private String SQLSentenceFK = "";

    public String getNombreclase() {
        return nombreclase;
    }

    public void setNombreclase(String nombreclase) {
        this.nombreclase = nombreclase;
    }

    public String getInheritance() {
        return inheritance;
    }

    public void setInheritance(String inheritance) {
        this.inheritance = inheritance;
    }

    public boolean isAutoPk() {
        return autoPK;
    }

    public void setAutoPk(boolean hasPK) {
        this.autoPK = hasPK;
    }

    public ArrayList<String> getAssembledClass() {
        return assembledClass;
    }

    public void setAssembledClass(ArrayList<String> assembledClass) {
        this.assembledClass = assembledClass;
    }

    public ArrayList<String> getAssembledMethods() {
        return assembledMethods;
    }

    public void setAssembledMethods(ArrayList<String> assembledMethods) {
        this.assembledMethods = assembledMethods;
    }

    public ArrayList<AbstractProperty> getProperties() {
        return properties;
    }

    public void setProperties(ArrayList<AbstractProperty> properties) {
        this.properties = properties;
    }

    public ArrayList<AbstractFK> getFks() {
        return fks;
    }

    public String getSQLSentenceAdd() {
        return SQLSentenceAdd;
    }

    public void setSQLSentenceAdd(String SQLSentenceAdd) {
        this.SQLSentenceAdd = SQLSentenceAdd;
    }

    public String getSQLSentenceCreate() {
        return SQLSentenceCreate;
    }

    public void setSQLSentenceCreate(String SQLSentenceCreate) {
        this.SQLSentenceCreate = SQLSentenceCreate;
    }

    public String getSQLSentenceDelete() {
        return SQLSentenceDelete;
    }

    public void setSQLSentenceDelete(String SQLSentenceDelete) {
        this.SQLSentenceDelete = SQLSentenceDelete;
    }

    public String getSQLSentenceModify() {
        return SQLSentenceModify;
    }

    public void setSQLSentenceModify(String SQLSentenceModify) {
        this.SQLSentenceModify = SQLSentenceModify;
    }

    public String getSQLSentenceFK() {
        return SQLSentenceFK;
    }

    public void setSQLSentenceFK(String SQLSentenceFK) {
        this.SQLSentenceFK = SQLSentenceFK;
    }

    public ArrayList<AbstractProperty> returnPK() {
        ArrayList<AbstractProperty> prop = null;
        for (AbstractProperty p : this.properties) {
            if (p.isPk()) {
                prop.add(p);
            }
        }
        return prop;
    }

    public AbstractProperty readProp(String name) {
        AbstractProperty prop = null;
        for (AbstractProperty p : this.properties) {
            if (p.getName().equals(name)) {
                prop = p;
            }
        }
        return prop;
    }

    public ArrayList<AbstractProperty> readPKs() {
        ArrayList<AbstractProperty> pks = new ArrayList<AbstractProperty>();
        for (AbstractProperty p : getProperties()) {
            if (p.isPk()) {
                pks.add(p);
            }
        }
        return pks;
    }

    public void deleteProp(String prop) {
        int i = 0;
        while (i < this.getProperties().size()) {
            if (getProperties().get(i).getName().equals(prop)) {
                this.properties.remove(i);
            }
            i++;
        }
    }

    public void clearProps (){
        this.getProperties().clear();
    }

    public boolean addProperty(EnumDataType type, String name, boolean pk, String collection) {
        boolean res = false;
        AbstractProperty prop = new AbstractProperty();
        if (!collection.equals("Collection")) {
            res = true;
            prop.setCollection(collection);
        }
        prop.setName(name);
        prop.setPk(pk);
        prop.setType(type);
        getProperties().add(prop);
        return res;
    }

    public void addFK(EnumDataType datatype, String property, String reftable, String fkd) {
        AbstractFK fk = new AbstractFK();
        fk.setFk(fkd);
        fk.setProperty(property);
        fk.setRefTable(reftable);
        fk.setType(datatype);
        this.getFks().add(fk);
    }

    public AbstractFK searchFK(String fk) {
        AbstractFK sfk = null;
        for (AbstractFK f : getFks()) {
            if (f.getProperty().equals(fk)) {
                sfk = f;
            }
        }
        return sfk;
    }

    public void deleteFK(String fk) {
        int i = 0;
        while (i < this.getFks().size()) {
            if (getFks().get(i).getProperty().equals(fk)) {
                this.fks.remove(i);
            }
            i++;
        }
    }

    public void clearFKs (){
        this.getFks().clear();
    }

    public boolean hasTableinFks(String classname) {
        boolean res = false;
        for (AbstractFK fk : getFks()) {
            if (fk.getRefTable().equals(classname)) {
                res = true;
            }
        }
        return res;
    }

    public ArrayList<AbstractFK> getFksByTable(String classname) {
        ArrayList<AbstractFK> newfks = new ArrayList<AbstractFK>();
        for (AbstractFK fk : getFks()) {
            if (fk.getRefTable().equals(classname)) {
                newfks.add(fk);
            }
        }
        return newfks;
    }

    public boolean checkCollectionTableFKs(String classname) {
        //Chequea si todas las fks de una tabla son una colección. Sino devuelve false. 
        //(Forzosamente tienen que ser todas una colección)
        boolean res = false;
        int cantfks = 0;
        int cantfkscol = 0;
        for (AbstractFK fk : getFks()) {
            if (fk.getRefTable().equals(classname)) {
                cantfks++;
                if (fk.getType() == EnumDataType.COLLECTION) {
                    cantfkscol++;
                }
            }
        }
        if (cantfks == cantfkscol) {
            res = true;
        }
        return res;

    }

    public void closeSQLSentences() {
        this.SQLSentenceCreate += ");";
        this.SQLSentenceAdd += ");";
        this.SQLSentenceDelete += ");";
        this.SQLSentenceModify += ");";
        this.SQLSentenceFK += ");";
    }

    @Override
    public String toString() {
        return getNombreclase() + " - Pk? :" + isAutoPk() + " - Hereda de:" + getInheritance();
    }
}
